<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Goods;

class GoodsController extends Controller
{   
    /**
     * 商品详情上部
     *
     * @param Request $request
     * @return void
     */
    public function index( Request $request )
    {
        $id    = $request->input('id');
 
        // 查询的字段
        $field = ['id', 'retail_price', 'name', 'market_price', 'pic', 'sales', 'num'];

        $info  = Goods::with(['norm' => function ( $query ) {
            $query->select('id', 'name', 'goods_id');
        }])->where('status', 1)->select($field)->where('is_sell', 1)->find($id);
 
        if ( !$info ) {
            return self::error('请传递正确的商品');
        }
       
        $info->makeVisible(['pics', 'discount', 'share']);
       
        $info['url'] = url('api/goods/content?id='. $id);

        return self::json($info);
    }

    /**
     * 商品详情下部
     *
     * @return void
     */
    public function content( Request $request )
    {
        $id = $request->input('id');
        $content = Goods::where('id', $id)->value('content');
       
        header('Content-type: text/html; charset=utf-8');

        $html  = '<html lang="zh-CN">
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=device-width, initial-scale=1">
                  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
                  <style>img{width: 100% !important; height: auto !important }</style>';

        $html .= htmlspecialchars_decode($content);
        $html .= '</html>';
        echo $html;
    }
}
